package com.sunplus.system.registration.controller;

import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.sunplus.system.registration.domain.PortalArticle;
import com.sunplus.system.registration.service.IPortalArticleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.sunplus.common.annotation.Log;
import com.sunplus.common.core.controller.BaseController;
import com.sunplus.common.core.domain.AjaxResult;
import com.sunplus.common.enums.BusinessType;
import com.sunplus.common.utils.poi.ExcelUtil;

/**
 * 文章管理Controller
 * 
 * @author sunplus
 * @date 2021-03-25
 */
@RestController
@RequestMapping("/registration/articleMgr")
public class PortalArticleController extends BaseController
{
    @Autowired
    private IPortalArticleService portalArticleService;

    /**
     * 查询文章管理列表
     */
//    @PreAuthorize("@ss.hasPermi('system:article:list')")
    @PostMapping("/listPain")
    public AjaxResult list(PortalArticle portalArticle)
    {
        startPage();
        List<PortalArticle> list = portalArticleService.selectPortalArticleList(portalArticle);
        JSONObject data = getDataTable(list);
        data.put("ButtonRight", "add,edit,del,view,SetRole,updatepassword");
        return AjaxResult.success(data);
    }

    /**
     * 导出文章管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:article:export')")
    @Log(title = "文章管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(PortalArticle portalArticle)
    {
        List<PortalArticle> list = portalArticleService.selectPortalArticleList(portalArticle);
        ExcelUtil<PortalArticle> util = new ExcelUtil<PortalArticle>(PortalArticle.class);
        return util.exportExcel(list, "article");
    }

    /**
     * 获取文章管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:article:query')")
    @GetMapping(value = "/{pid}")
    public AjaxResult getInfo(@PathVariable("pid") String pid)
    {
        return AjaxResult.success(portalArticleService.selectPortalArticleById(pid));
    }
    /**
     * 获取按钮
     *
     * @return
     * @throws Exception
     */
    @PostMapping("/GetButtonRight")
    public AjaxResult getButtonRight() throws Exception {
        return AjaxResult.success((Object) "<button class=\"layui-btn layuiadmin-btn-list\" data-type=\"add\">添加</button><button class=\"layui-btn layuiadmin-btn-list\" data-type=\"del\">删除</button>");
    }
    /**
     * 新增文章管理
     */
//    @PreAuthorize("@ss.hasPermi('system:article:add')")
    @Log(title = "文章管理", businessType = BusinessType.INSERT)
    @PostMapping("/Add")
    public AjaxResult add( PortalArticle portalArticle)
    {
        return toAjax(portalArticleService.insertPortalArticle(portalArticle));
    }

    /**
     * 修改文章管理
     */
//    @PreAuthorize("@ss.hasPermi('system:article:edit')")
    @Log(title = "文章管理", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult edit( PortalArticle portalArticle)
    {
        return toAjax(portalArticleService.updatePortalArticle(portalArticle));
    }

    /**
     * 删除文章管理
     */
//    @PreAuthorize("@ss.hasPermi('system:article:remove')")
    @Log(title = "文章管理", businessType = BusinessType.DELETE)
    @PostMapping("/BatchDelete")
    public AjaxResult remove(@RequestParam String[] ids)
    {
        return toAjax(portalArticleService.deletePortalArticleByIds(ids));
    }
}
